package at.hockey.ticker;

import java.util.ListIterator;

import at.hockey.ticker.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MatchDetailsUI extends LinearLayout {

	private HockeyTickerUI mHTUI;
	private MatchDetails mMD;
	private LinearLayout mLL;

	public MatchDetailsUI(HockeyTickerUI root, MatchDetails md) {
		super(root);
		
		this.mHTUI = root;
		
		//instantiate xml layout
		LayoutInflater inflater = (LayoutInflater) this.mHTUI.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.matchdetails, this);

		this.mLL = (LinearLayout) this.findViewById(R.id.md_entries_ll);

		this.mMD = md;
		
		int lastPeriod = 0;
		MatchDetailsEntry mde = null;
		ListIterator<MatchDetailsEntry> itr = md.getMatchDetailsEntries().listIterator();
	    while(itr.hasNext()){

	    	mde = itr.next();
	    	
	    	if(lastPeriod != mde.getPeriod()){
	    		this.addPeriodDivider(mde.getPeriod());
	    	}
	    	lastPeriod = mde.getPeriod();

	    	this.mLL.addView(new MatchDetailsEntryUI(this.mHTUI, md, mde));
	    }
	}

    private void addPeriodDivider(int period){

		//instantiate xml layout
    	LayoutInflater inflater = (LayoutInflater) this.mHTUI.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TextView tv = (TextView) inflater.inflate(R.layout.divider, null);
    	
		tv.setText("Period " + period);

    	this.mLL.addView(tv);
    }
    
    public MatchDetails getMatchDetails(){
    	return this.mMD;
    }
}
